package city.spring.modules.ext.service.impl;

import city.spring.modules.ext.entity.MealFoodExt;
import city.spring.modules.ext.repository.MealFoodExtRepository;
import city.spring.modules.ext.service.MealFoodExtService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.ArrayListMultimap;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

@Service
public class MealFoodExtServiceImpl extends ServiceImpl<MealFoodExtRepository, MealFoodExt> implements MealFoodExtService {

    @Override
    public List<String> getMealTurn(String mealWeekId) {
        List<String> mealTurn = baseMapper.getMealTurnByMealWeekId(mealWeekId);
        HashSet h = new HashSet(mealTurn);
        mealTurn.clear();
        mealTurn.addAll(h);
        return mealTurn;
    }

    @Override
    public List<Integer> getVotes(String mealWeekId) {
        List<MealFoodExt> exts = lambdaQuery().eq(MealFoodExt::getMealWeekId, mealWeekId).list();
        List<Integer> votes = new LinkedList<>();
        String mealTurn = null;
        if (exts.size() > 0) {
            mealTurn = exts.get(0).getMealTurn();
            votes.add(0, exts.get(0).getVotes());
            for (int i = 0; i < exts.size(); i++) {
                if (!exts.get(i).getMealTurn().equals(mealTurn)) {
                    mealTurn = exts.get(i).getMealTurn();
                    votes.add(votes.size(), exts.get(i).getVotes());
                }
            }
        }
        return votes;
    }

    @Override
    public ArrayListMultimap<String, String> getMealTurn(Set<String> newWeekMealIds) {
        List<MealFoodExt> mealFoodExts = lambdaQuery().in(MealFoodExt::getMealWeekId, newWeekMealIds).list();
        ArrayListMultimap<String, String> multimap = ArrayListMultimap.create();
        for (MealFoodExt foodExt : mealFoodExts) {
            multimap.put(foodExt.getMealWeekId(), foodExt.getMealTurn());
        }
        return multimap;
    }

    @Override
    public List<MealFoodExt> getMealFoodExt(String mealWeekId, String mealTurn) {
        return baseMapper.getMealFoodEntity(mealWeekId, mealTurn);
    }

}
